package top.byteio.common.mq.factory;

import top.byteio.common.cache.lock.DistributedLock;
import top.byteio.common.core.exception.ServiceException;
import top.byteio.common.mq.exception.MqException;
import top.byteio.common.mq.handler.MqProducerSender;
import top.byteio.common.mq.pojo.dto.MqDTO;
import top.byteio.common.mq.pojo.dto.MqResultDTO;

/**
 * MQ消息工厂
 * @author lerryxia
 **/
public abstract class AbstractMqFactory<T> {

    protected  DistributedLock distributedLock;

    protected MqProducerSender mqProducerSender;

    public AbstractMqFactory(DistributedLock distributedLock ,MqProducerSender mqProducerSender) {
        this.mqProducerSender = mqProducerSender;
        this.distributedLock = distributedLock;
    }

    /**
     * 发送mq消息
     *
     * @param mqDto 消息体
     * @return 结果
     * @throws MqException mq异常
     */
    protected MqResultDTO sendMq(MqDTO<T> mqDto) throws MqException {
        return mqProducerSender.sendMq(mqDto);
    }

    /**
     * 消息监听
     * @param value 消息体
     * @throws MqException mq异常
     */
    protected abstract void listener(String value) throws MqException;

    /**
     * 业务消费处理
     * @param mqDto 消息体
     * @throws MqException  mq异常
     * @throws ServiceException 业务异常
     * @author lerryxia
     **/
    protected abstract void consumeMq(MqDTO<T> mqDto) throws ServiceException, MqException;

}
